﻿using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.WebApi.Controllers
{
    [ApiController]
    public class CustomerController : ControllerBase
    {

        private readonly IMediator _mediator;
        private readonly ICustomerQueries _customerQueries;

        public CustomerController(IMediator mediator,
            ICustomerQueries unitQueries)
        {
            _mediator = mediator;
            _customerQueries = unitQueries;
        }

        [HttpGet(ApiRoutes.Customer.QueryCustomers)]
        public async Task<IActionResult> Get([FromQuery] CustomerSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var result = await _customerQueries.QueryCustomersByPage(model);
                return Ok(result);
            }
            else
            {
                var result = await _customerQueries.QueryCustomers(model);
                return Ok(result);
            }
        }

        [HttpGet(ApiRoutes.Customer.GetCustomer)]
        public async Task<IActionResult> GetCustomer(int id)
        {
            var result = await _customerQueries.GetCustomer(id);
            return Ok(result);
        }

        [HttpPost(ApiRoutes.Customer.CreateCustomer)]
        public async Task<IActionResult> Post(CreateCustomerCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.Customer.UpdateCustomer)]
        public async Task<IActionResult> Put(int id, [FromBody] UpdateCustomerCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}